package com.alfred.mall.miaosha.controller;

import com.alfred.common.framwork.constant.MiaoshaConstant;
import com.alfred.common.framwork.exception.ServiceException;
import com.alfred.common.framwork.vo.CommonResult;
import com.alfred.mall.miaosha.api.MiaoshaGoodsServcie;
import com.alfred.mall.miaosha.api.bo.MiaoshaGoodsBO;
import com.alfred.mall.miaosha.api.dto.MiaoshaOrderRequestDTO;
import com.alfred.mall.miaosha.api.dto.MiaoshaRequestDTO;
import com.alfred.mall.miaosha.api.dto.MiaoshaResultRequestDTO;
import com.alfred.mall.miaosha.api.limit.AccessLimitService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 秒杀接口
 */
@Controller
@RequestMapping("interface")
public class MiaoshaInterfaceController {

    @Reference
    AccessLimitService accessLimitService;
    @Reference(validation = "true")
    MiaoshaGoodsServcie miaoshaGoodsServcie;


    /**
     * 秒杀入口
     *
     * @param miaoshaRequestDTO
     * @return
     */
    @RequestMapping(value = "startSeckill")
    @ResponseBody
    public CommonResult startSeckill(@RequestBody @Validated MiaoshaRequestDTO miaoshaRequestDTO) {
        //1、首先限流-guava
        if (!accessLimitService.tryAcquireSeckill()) {
            throw new ServiceException(MiaoshaConstant.MIAOSHA_OUT.getCode(), MiaoshaConstant.MIAOSHA_OUT.getMessage());
        }
        miaoshaGoodsServcie.seckill(miaoshaRequestDTO);
        return CommonResult.success(miaoshaRequestDTO.getMobile());
    }


    /**
     * 获取秒杀商品链接
     *
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "{goodsId}/getSeckillGoodsLink")
    @ResponseBody
    public CommonResult getSeckillGoodsLink(@PathVariable("goodsId") Integer goodsId) {
        MiaoshaGoodsBO goods = miaoshaGoodsServcie.selectMiaoshaGoodsById(goodsId);
        if (null != goods) {
            long now = System.currentTimeMillis();
            //商品秒杀开始时间小于当前当前系统时间并且当前系统时间小于商品秒杀结束时间
            //满足以上条件，开启秒杀入口
            if (goods.getStartTime().getTime() < now && now < goods.getEndTime().getTime()) {
                return CommonResult.success(goods.getRandomName());
            }
        }
        return CommonResult.error(MiaoshaConstant.MIAO_SHA_OVER.getCode(), MiaoshaConstant.MIAO_SHA_OVER.getMessage());
    }


    /**
     * 获取秒杀结果，拿到秒杀下单token
     *
     * @param resultRequestDTO
     * @return
     */
    @RequestMapping(value = "/seckillResult")
    @ResponseBody
    public CommonResult seckillResult(@RequestBody @Validated MiaoshaResultRequestDTO resultRequestDTO) {
        return CommonResult.success(miaoshaGoodsServcie.queryToken(resultRequestDTO));
    }


    /**
     * 真正的下单，操作DB
     *
     * @param orderRequestDTO
     * @return
     */
    @RequestMapping(value = "/order")
    @ResponseBody
    public CommonResult order(@RequestBody @Validated MiaoshaOrderRequestDTO orderRequestDTO) {
        return CommonResult.success(miaoshaGoodsServcie.order(orderRequestDTO));
    }

    /**
     * 获取当前系统时间(毫秒)
     *
     * @return
     */
    @RequestMapping("time/now")
    @ResponseBody
    public CommonResult getTime() {
        long currentSystemTime = System.currentTimeMillis();
        return CommonResult.success(currentSystemTime);
    }
}
